﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ODS.WebService.DeviceCommand;
using ODS.Utils;
using ODS.Config;

namespace ODS.UserControls
{
    public partial class StarterActionPage : UserControl
    {
        public DetailDevicePage ParentView;

        public StarterActionPage()
        {
            InitializeComponent();

            if (App.currentDeviceModel.IsStarterOn)
            {
                btn_activeImmobilize.ImageSource = "/Assets/images/Activate starter kill button.png";
                btn_activeImmobilize.ImagePressedSource = "/Assets/images/Activate starter kill button.png";
            }
            else
            {
                btn_activeImmobilize.ImageSource = "/Assets/images/Activate button.png";
                btn_activeImmobilize.ImagePressedSource = "/Assets/images/Activate button.png";
            }
        }

        private async void btn_activeImmobilize_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            String errorMessage = null;
            int commandType ;
            if (App.currentDeviceModel.IsStarterOn)
                commandType = 13;
            else
                commandType = 14;

            InfoResult result_requestCommand = await RequestCommandService.RequestCommand(App.currentDeviceModel.DeviceIMEI, commandType, null);

            if (result_requestCommand.IsSuccess)
            {
                String message = result_requestCommand.ReturnedObject as String;
                if (message == Constants.SUCCESS)
                {
                    String mess;
                    if (App.currentDeviceModel.IsStarterOn)
                    {
                        App.currentDeviceModel.IsStarterOn = false;
                        mess = "Starter-off";
                    }
                    else
                    {
                        App.currentDeviceModel.IsStarterOn = true;
                        mess = "Starter-on";
                    }

                    MessageBox.Show(mess);
                }
                else
                {
                    errorMessage = "Request processing error. Please try again!";
                }
            }
            else
            {
                errorMessage = result_requestCommand.Error;
            }

            if (errorMessage != null)
            {
                MessageBox.Show(errorMessage);
            }
        }
    }
}
